package com.scanner.ui;


import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.scanner.hardware.R;


public class AppVersionDialog extends DialogFragment {

    private OnSureDownLoadListener listener;
    private TextView tvContent;
    private TextView tvSize;
    private View inflate;
    private String size;
    private String content;
    private int type;

    public static AppVersionDialog getInstance(int type){
        AppVersionDialog depotDialog = new AppVersionDialog();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        depotDialog.setArguments(bundle);
        return depotDialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        assert getArguments() != null;
        type = getArguments().getInt("type");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//设置dialog没有title
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomProgressDialog);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if(inflate == null){
            inflate = inflater.inflate(R.layout.dialog_app_update_tip, container, false);
        }
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvContent = view.findViewById(R.id.tv_update_content);
        tvSize = view.findViewById(R.id.tv_update_size);
        Button btnCancel =  view.findViewById(R.id.btn_cancel);
        if(size!= null){
            tvSize.setText(String.format(inflate.getContext().getResources().getString(R.string.app_size), size));
        }
        if(tvContent!=null){
            tvContent.setText(TextUtils.isEmpty(content) ? "" : content);
        }
        if(type == 2){
            btnCancel.setVisibility(View.GONE);
        }
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissDialog();
            }
        });

        view.findViewById(R.id.btn_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onClick();
                }
                //下载
                dissDialog();
            }
        });
    }

    public AppVersionDialog setContent(String content){
        this.content = content;
        return this;
    }


    public AppVersionDialog setSize(String size){
        this.size = size;
        return this;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        Window dialogWindow = getDialog().getWindow();
        assert dialogWindow != null;
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);

    }


    public void dissDialog(){
        if(getDialog()!= null && getDialog().isShowing()){
            getDialog().dismiss();
        }
    }

    public interface OnSureDownLoadListener{
        void onClick();
    }

    public void addOnSureListener(OnSureDownLoadListener listener){
        this.listener = listener;
    }


    @Override
    public void show(FragmentManager manager, String tag) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        // 这里吧原来的commit()方法换成了commitAllowingStateLoss()
        ft.commitAllowingStateLoss();
    }

}
